package com.yunyike.app.data.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yunyike.app.data.mapper.MerchandiseMapper;
import com.yunyike.app.data.model.Merchandise;
import com.yunyike.app.data.service.IMerchandiseService;
import com.yunyike.app.util.maybatis.PageBean;

@Service("merchandiseServiceImpl")
public class MerchandiseServiceImpl implements IMerchandiseService{

private static Logger log = LoggerFactory.getLogger(MerchandiseServiceImpl.class);
	
	@Autowired
	private MerchandiseMapper merchandiseMapper;

	/*
	 * 添加商品
	 */
	public int addMerchandise(Merchandise merchandise) {
		log.info("----->> add merchandise....");
		
		int result = 0;
		try {
			result = merchandiseMapper.insertSelective(merchandise);
		} catch (Exception e) {
			log.info("add merchandise has errors,message:"+e.getMessage(),e);
		}
		
		log.info("add merchandise end .result:"+result+"<<----");
		
		return result;
	}

	/*
	 * 根据商品编号 获取商品信息
	 */
	public Merchandise findMerchandiseByNumber(String number) {
		log.info("----->> find merchandise....");
		
		Merchandise merchandise = null;
		try {
			merchandise = merchandiseMapper.findMerchandiseByNumber(number);
		} catch (Exception e) {
			log.info("find merchandise has errors,message:"+e.getMessage(),e);
		}
		
		log.info("find merchandise end .");
		
		return merchandise;
	}

	/**
	 * 根据商品id获取商品信息（多个）
	 */
	public List<Merchandise> findMerchandiseByIds(
			List<Merchandise> merchandiseList) {
		
		Map<String, Object> map = getAllMerchandiseId(merchandiseList);
		
		merchandiseList = merchandiseMapper.findMerchandiseByIds(map);
		
		return merchandiseList;
	}
	
	/**
	 * 获取商品id map
	 * @param merchandiseList
	 * @return
	 */
	private Map<String, Object> getAllMerchandiseId(List<Merchandise> merchandiseList){
		
		List<String> merchandiseIdList = new ArrayList<String>();
		for (Merchandise merchandise : merchandiseList) {
			merchandiseIdList.add(merchandise.getMerchandiseId());
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("merchandiseIdList", merchandiseIdList);
		
		return map;
	}

	/**
	 * 获取店铺的商品
	 */
	public List<Merchandise> findMerchandiseByShop(String shopId) {
		
		log.info("findMerchandiseByShop,shopId:"+shopId);
		
		List<Merchandise> findMerchandiseByShop = null;
		try {
			PageBean<String> pageBean = new PageBean<String>();
			pageBean.setParameter(shopId);
			
			findMerchandiseByShop = merchandiseMapper.findMerchandiseListPageByShop(pageBean);
		} catch (Exception e) {
			log.error("find merchandise by shop failed.message:"+e.getMessage(),e);
			return null;
		}
		
		if(findMerchandiseByShop == null || findMerchandiseByShop.isEmpty()){
			log.info("current shop no merchandise.");
			return null;
		}
		
		log.info("findMerchandiseByShop end. count:"+findMerchandiseByShop.size());
		
		return findMerchandiseByShop;
	}

	/*
	 * 获取符合重量的商品
	 */
	public List<Merchandise> findMerchandiseByCondition(String shopId,
			double weight) {
		
		log.info("findMerchandiseByCondition... ");
		
		// 如：2.5公斤ceil后为3公斤 减去2.5公斤 = 0.5公斤 再乘以1000 得 500克
		weight = (Math.ceil(weight) - weight)*1000;
		
		log.info("shopId:"+shopId+"\tweight:"+weight);
		
		Merchandise mer = new Merchandise();
		mer.setShopId(shopId);
		mer.setMerchandiseWeight(weight);
		
		log.info("call db...");
		List<Merchandise> list = merchandiseMapper.selectBySelective(mer);
		
		if(list == null || list.isEmpty()){
			log.info("call db end.no record.");
			return null;
		}
		
		log.info("call db end.count:"+list.size());
		
		return list;
	}

	public List<Merchandise> findMerchandises() {
		return merchandiseMapper.findAllMerchandises();
	}

	public int updateMerchandise(Merchandise merchandise) {
		return merchandiseMapper.updateByPrimaryKeySelective(merchandise);
	}

	/**
	 * 根据商品条形码 获取商品信息
	 */
	public Merchandise findMerchandiseByBarCode(String barCode) {
		return merchandiseMapper.findMerchandiseByBarCode(barCode);
	}
	public Merchandise findMerchandiseById(String merchandiseId){
		return merchandiseMapper.selectByPrimaryKey(merchandiseId);
	}
	
}
